Skip to content

Added cholesky inverse operation to all the backends #21554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Aug 12, 2025

Conversation

buildwithsuhana
Copy link
Contributor

@buildwithsuhana buildwithsuhana commented Aug 7, 2025

The Cholesky inverse is a fast and numerically stable algorithm for calculating the inverse of a special type of matrix: one that is symmetric and positive-definite.

Instead of calculating the inverse directly, this method first breaks the matrix down into simpler components using Cholesky decomposition. This approach is generally more efficient and less prone to floating-point errors than a general-purpose inversion method.

Its numerical stability makes it a key component in advanced machine learning algorithms like GPTQ quantisation, where it is used to efficiently compute the inverse of Hessian matrices for layer-wise weight compression.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @buildwithsuhana, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request expands Keras's linear algebra capabilities by adding a cholesky_inverse function. This addition provides a robust and efficient method for matrix inversion, crucial for various numerical computations, and ensures consistent behavior across different backend frameworks.

Highlights

  • New Linear Algebra Operation: I've introduced a cholesky_inverse operation, which provides a numerically stable and efficient way to compute the inverse of symmetric positive-definite matrices. This method first performs a Cholesky decomposition and then uses triangular solves to find the inverse.
  • Backend Implementations: The cholesky_inverse function has been implemented across multiple Keras backends: JAX, NumPy, TensorFlow, and PyTorch. For OpenVINO, it's currently marked as not supported.
  • Keras Ops Integration and Testing: The new operation is integrated into keras.ops.linalg, supporting symbolic tensors and including input validation for 2D and square matrices. Corresponding unit tests have been added to linalg_test.py to verify its behavior and error handling.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the cholesky_inverse operation across various Keras backends. The implementation is a welcome addition, providing a more efficient and numerically stable method for inverting symmetric positive-definite matrices compared to a general-purpose inverse. My review has identified a few issues, including critical bugs in the JAX and NumPy backend implementations related to handling batched inputs, which could lead to incorrect results. I've also noted a lack of correctness tests and a missing docstring, which are important for maintainability and usability. I've provided specific suggestions to address these points. Once these issues are resolved, this will be a solid contribution to the Keras API.

Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, quick review! Let's run code formatting and linting. Here are the instructions: #21554 (comment).

buildwithsuhana and others added 3 commits August 7, 2025 10:46
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Left a few comments

Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@gbaned gbaned added this to PR Queue Aug 11, 2025
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Aug 11, 2025
@abheesht17
Copy link
Collaborator

@buildwithsuhana - can you set up pre-commit: https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md#generating-public-api-and-formatting-the-code? This will ensure that your changes are formatted, and the symbols you've added are exported

Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left small comments, will do one final pass in a bit. Thanks!

@codecov-commenter
Copy link

codecov-commenter commented Aug 11, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.75%. Comparing base (6114a21) to head (a70b69c).
⚠️ Report is 11 commits behind head on master.

Files with missing lines Patch % Lines
keras/src/ops/linalg.py 87.50% 4 Missing ⚠️
keras/api/_tf_keras/keras/ops/__init__.py 0.00% 1 Missing ⚠️
keras/api/_tf_keras/keras/ops/linalg/__init__.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #21554      +/-   ##
==========================================
+ Coverage   82.73%   82.75%   +0.02%     
==========================================
  Files         567      567              
  Lines       56456    56528      +72     
  Branches     8823     8823              
==========================================
+ Hits        46710    46782      +72     
- Misses       7582     7585       +3     
+ Partials     2164     2161       -3     
Flag Coverage Δ
keras 82.56% <91.66%> (+0.02%) ⬆️
keras-jax 63.77% <62.50%> (-0.04%) ⬇️
keras-numpy 58.29% <62.50%> (-0.01%) ⬇️
keras-openvino 34.66% <25.00%> (+0.02%) ⬆️
keras-tensorflow 64.22% <66.66%> (-0.03%) ⬇️
keras-torch 63.81% <55.55%> (-0.06%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Collaborator

@abheesht17 abheesht17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good to me :)

Left one nit, let's merge it after that!

@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Aug 12, 2025
@abheesht17
Copy link
Collaborator

@hertschuh - could you please drop an approval here? I don't have write access.

@google-ml-butler google-ml-butler bot removed the ready to pull Ready to be merged into the codebase label Aug 12, 2025
@google-ml-butler google-ml-butler bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Aug 12, 2025
@github-project-automation github-project-automation bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Aug 12, 2025
@hertschuh hertschuh merged commit cd7ec31 into keras-team:master Aug 12, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from Approved by Reviewer to Merged in PR Queue Aug 12, 2025
@google-ml-butler google-ml-butler bot removed awaiting review ready to pull Ready to be merged into the codebase labels Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Merged
Development

Successfully merging this pull request may close these issues.

6 participants